package StudentControl.StudentManager;

import Domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentIn {
    private ArrayList<Student> stuList;
    //Constructor==================================================================
    public StudentIn() {
    }

    public StudentIn(ArrayList<Student> stuList) {
        this.stuList = stuList;
    }
    //Getter and Setter===========================================================
    public ArrayList<Student> getStuList() {
        return stuList;
    }

    public void setStuList(ArrayList<Student> stuList) {
        this.stuList = stuList;
    }
    //Method====================================================================
    public void studentLogin(ArrayList<Student> studentsList){
        Scanner sc = new Scanner(System.in);
        loop:while (true) {
            System.out.println("============欢迎来到黑马学生管理系统============");
            System.out.println("1.查看信息");
            System.out.println("2.修改信息");
            System.out.println("3.返回上一级");
            System.out.println("4.退出");
            System.out.println("请输入您的选择：");
            String choice = sc.nextLine();
            //确认情况
            switch (choice) {
                case "1"://查看信息
                    queryMine(studentsList);
                    break;
                case "2"://修改信息
                    updateMine(studentsList);
                    break;
                case "3":
                    System.out.println("返回上一级");
                    break loop;
                case "4":
                    System.out.println("谢谢您的使用");
                    System.exit(0);
                default:
                    System.out.println("您输入的信息有误，请重新输入：");
            }
        }
    }

    private void updateMine(ArrayList<Student> studentsList) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您的学号：");
        //1.查询学生学号是否存在，存在则允许修改信息，不存在返回。
        String id = sc.nextLine();
        int index=-1;
        for (int i = 0; i < studentsList.size(); i++) {
            Student stu = studentsList.get(i);
            if(stu.getId().equals(id)){
                index = i;
                break;
            }
        }

        if(index == -1) {
            System.out.println("您的学号不存在！");
            return;
        }

        Student stu = studentsList.get(index);
        loop:while (true) {
            System.out.println("请问您要修改哪一项：");
            System.out.println("1.年龄");
            System.out.println("2.生日");
            System.out.println("请选择：");
            String choice = sc.nextLine();
            switch (choice){
                case "1":
                    System.out.println("请输入年龄：");
                    int age = sc.nextInt();
                    stu.setAge(age);
                    System.out.println("修改完成！");
                case "2":
                    System.out.println("请输入年龄：");
                    String birthday = sc.nextLine();
                    stu.setBirthday(birthday);
                    System.out.println("修改完成！");
                case "3":
                    System.out.println("返回上一级");
                    break loop;
                default:
                    System.out.println("您的输入有误，请重新输入：");
                    break;
            }
        }

    }

    //查询自身信息
    private void queryMine(ArrayList<Student> studentsList) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您的学号：");
        //1.查询学生学号是否存在，存在则展示信息，不存在返回。
        String id = sc.nextLine();
        int index=-1;
        for (int i = 0; i < studentsList.size(); i++) {
            Student stu = studentsList.get(i);
            if(stu.getId().equals(id)){
                index = i;
                break;
            }
        }

        if(index == -1) {
            System.out.println("您的学号不存在！");
            return;
        }else{
            System.out.println("姓名   \t学号   \t年龄\t     生日");
            Student stu = studentsList.get(index);
            System.out.println(stu.getName()
                + "\t" + stu.getId()
                + "\t" + stu.getAge()
                + "\t" + stu.getBirthday());
        }
    }
}
